﻿
Imports osi.root.connector
Imports osi.root.template

Public Class dynamic_pool(Of T, _THREADSAFE As _boolean, _RNDSELECT As _boolean)
    Inherits pool(Of T, _THREADSAFE, _RNDSELECT, _true)
    Private ReadOnly p As vector(Of pair(Of pointer(Of T), Boolean)) = Nothing

    Protected Overrides Property at(ByVal index As Int64) As T
        Get
            Return +(p(index).first)
        End Get
        Set(ByVal value As T)
            p(index).first.set(value)
        End Set
    End Property

    Protected Overrides Property free(ByVal index As Int64) As Boolean
        Get
            Return p(index).second
        End Get
        Set(ByVal value As Boolean)
            k_assert(Not p(index).second)
            p(index).second = value
        End Set
    End Property

    Protected Overrides Function size() As Int64
        Return p.size()
    End Function

    Protected Overrides Function append() As Int64
        p.push_back(emplace_make_pair(New pointer(Of T)(create()), False))
        Return p.size() - 1
    End Function

    Public Sub New()
        MyBase.New()
        p = New vector(Of pair(Of pointer(Of T), Boolean))()
    End Sub

    Protected Overrides Sub Finalize()
        GC.KeepAlive(p)
        MyBase.Finalize()
    End Sub
End Class

Public Class dynamic_pool(Of T)
    Inherits dynamic_pool(Of T, _true, _false)
End Class
